import QtQuick 2.9
import QtQuick.Controls 2.2
import ToDo.module 1.0
import QtQuick.Controls.Styles 1.2
//import QtQuick.Controls.Material
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.4


ApplicationWindow {
    id: applicationWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")

    ListView {
        id:list_view;

        signal onConnectcppsignal;
        anchors.left: parent.left;
        anchors.top: parent.top-20;
        width: parent.width;
        height: parent.height-rectangle.height;
        flickableDirection: Flickable.AutoFlickDirection
        anchors.leftMargin: 0
        anchors.topMargin: 0
        model:m_listmodel;

        ListModel{
            id:m_listmodel;


            //   ListElement{name:"heloo"}

        }

        delegate: SwipeDelegate {
            id: delegate

            text: modelData
            width: parent.width

            //! [delegate]
            swipe.right: Rectangle {
                width: parent.width
                height: parent.height

                clip: true
                color: SwipeDelegate.pressed ? "#555" : "#666"

                Label {
                    font.family: "Fontello"
                    text: delegate.swipe.complete ? "\ue805" // icon-cw-circled
                                                  : "\ue801" // icon-cancel-circled-1

                    padding: 20
                    anchors.fill: parent
                    horizontalAlignment: Qt.AlignRight
                    verticalAlignment: Qt.AlignVCenter

                    opacity: 2 * -delegate.swipe.position

                    color: Material.color(delegate.swipe.complete ? Material.Green : Material.Red, Material.Shade200)
                    Behavior on color { ColorAnimation { } }
                }

                Label {
                    text: qsTr("Removed")
                    color: "white"

                    padding: 20
                    anchors.fill: parent
                    horizontalAlignment: Qt.AlignLeft
                    verticalAlignment: Qt.AlignVCenter

                    opacity: delegate.swipe.complete ? 1 : 0
                    Behavior on opacity { NumberAnimation { } }
                }

                SwipeDelegate.onClicked: delegate.swipe.close()
                SwipeDelegate.onPressedChanged: undoTimer.stop()
            }
            //! [delegate]

            //! [removal]
            Timer {
                id: undoTimer
                interval: 1500
                onTriggered: {

                    todo.todo_del(index);
                    console.log(index);
                    //  m_listmodel.remove(index);

                    m_listmodel.clear();
                }
            }

            swipe.onCompleted: undoTimer.start()
            //! [removal]
        }


        //! [transitions]
        remove: Transition {
            SequentialAnimation {
                PauseAnimation { duration: 125 }
                NumberAnimation { property: "height"; to: 0; easing.type: Easing.InOutQuad }
            }
        }

        displaced: Transition {
            SequentialAnimation {
                PauseAnimation { duration: 125 }
                NumberAnimation { property: "y"; easing.type: Easing.InOutQuad }
            }
        }
        //! [transitions]

        ScrollIndicator.vertical: ScrollIndicator { }

    }



    ToDo{

        id: todo

        onEmit_group_todo: {
            m_listmodel.clear();

        }
        onEmit_add_todo: {

            console.log("test");
            //添加待办
            m_listmodel.append({name:qsTr( todo_str)});

        }

    }


    Rectangle {
        id: rectangle
        x: 493
        y: 375
        width: 50
        height: 50
        color: "#ffffff"
        radius: 43
        anchors.right: parent.right
        anchors.rightMargin: 20
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 20
        border.width: 1
        rotation: 0

        MouseArea{
            anchors.fill: parent
            onClicked: {
                todo.todo_add(m_input.text);

                m_input.clear();

                console.log("click")
            }

        }


    }

    Image {
        id: image
        x: 540
        y: 380
        width: 60
        height: 60
        anchors.verticalCenter: rectangle.verticalCenter
        anchors.horizontalCenter: rectangle.horizontalCenter
        source: "image/add.png"
        fillMode: Image.PreserveAspectFit
    }

    TextInput{

        id:m_input;
        x: 10
        y: 263
        width: parent.width - rectangle.radius*2 -10
        horizontalAlignment: Text.AlignLeft
        font.pointSize: 14
        height: 40;
        color: "#c69191"
        autoScroll: true;
        text: qsTr("请输入")
        topPadding: 3
        font.wordSpacing: 0
        anchors.verticalCenter: rectangle.verticalCenter

        onFocusChanged: {

        m_input.clear();
        }
        Rectangle {
            id: rectangle1
            x: parent.left
            y: parent.top
            width: parent.width+1
            height: parent.height+1
            color: "#00000000"
            border.width: 2
        }

    }
}


/*##^##
Designer {
    D{i:20;anchors_height:100;anchors_width:100;anchors_x:540;anchors_y:380}D{i:21;anchors_x:0}
}
##^##*/
